<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <el-form label-position="top" :model="editForm" :rules="editFormRules" ref="editForm" @submit.native.prevent>
                <input type="hidden"  v-model="editForm.id">
                <el-form-item label="是否用户信息传输装置：">
                    <el-select filterable v-model="editForm.isDevice">
                      <el-option label="否" value="0"></el-option>
                      <el-option label="是" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否为边缘一体机：">
                    <el-select filterable v-model="editForm.isEdgeCalculator">
                      <el-option label="否" value="0"></el-option>
                      <el-option label="是" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="业主单位：" prop="organizationId">
                    <el-select filterable v-model="editForm.organizationId" >
                      <el-option
                        v-for="item in orgOptions"
                        :key="item.organizationId"
                        :label="item.shortName"
                        :value="item.organizationId">
                      </el-option>
                    </el-select>
                </el-form-item> -->
                <el-form-item label="通信服务器唯一标识：" prop="serverId">
                    <el-input v-model="editForm.serverId" auto-complete="off" placeholder="请输入唯一标识"></el-input>
                </el-form-item>
                <el-form-item v-if="editForm.isDevice==1" label="传输装置厂家标识：">
                    <el-select filterable v-model="editForm.factoryType">
                      <el-option
                        v-for="item in factoryTypeOptions"
                        :key="item.dictKey"
                        :label="item.dictValue"
                        :value="item.dictKey">
                      </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="型号：" prop="model">
                    <el-input v-model="editForm.model" auto-complete="off" placeholder="请输入型号"></el-input>
                </el-form-item>
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="联系电话：">
                    <el-input v-model="editForm.tel" auto-complete="off" placeholder="请输入联系电话"></el-input>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input type="textarea" v-model="editForm.remarks"></el-input>
                </el-form-item>
                <el-form-item label-width="0" class="verifyidentity-button">
                    <el-button round @click="closeDialog()">取消</el-button>
                    <el-button type="primary" round @click="saveEdit('editForm')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script>
import util from '@/utils/util'
import { getDictByCode} from '@/api/dic'
import { getOrgList} from '@/api/org'
import { qnToken} from '@/api/api'
import {update} from '@/api/dcx'

export default {
  name:'UserTransDeviceForm',
  props: {
    editInfo: {
        type: Object,
        required: false
    },
    closeDialog: {
        type: Function,
        required: true
    },
  },
  data() {
    return {
        defaultImg: this.GLOBAL.defaultImg,
        util:util,
        orgOptions: [],
        factoryTypeOptions: [{dictKey: "0",dictValue: "请选择厂家"}],
        editForm: {
            id: 0,
            serverId:'',
            organizationId:this.$store.state.userinfo.org.id,
            model:'',
            name:'',
            remarks:'',
            isDevice:'0',
            factoryType:'0',
            tel: '',
            isEdgeCalculator:'0'
        },
        editFormRules: {
            name: [
                { required: true, message: '请输入名称', trigger: 'blur' },
            ],
            serverId: [
                { required: true, message: '请输入唯一标识', trigger: 'blur' },
            ],
            model: [
                { required: true, message: '请输入型号', trigger: 'blur' },
            ],
            name: [
                { required: true, message: '请输入名称', trigger: 'blur' },
            ],
            organizationId: [
              { required: true, message: '请选择业主单位', trigger: 'change' }
            ],
        },
    }
  },
  created() {
      let that = this
      //绑定下拉框
      getOrgList({queryType: 0}).then((data)=>{
         this.orgOptions.push.apply(this.orgOptions,data.data);
      })
      getDictByCode({code: 85}).then((data)=>{
        this.factoryTypeOptions.push.apply(this.factoryTypeOptions,data.data.dictValueObj);
      })
      let infoObj = this.editInfo
      this.editForm.organizationId = infoObj.organizationId
      if(infoObj.id) {//编辑
        this.editForm.id = infoObj.id
        this.editForm.model = infoObj.model
        this.editForm.name = infoObj.name
        this.editForm.remarks = infoObj.remarks
        this.editForm.isDevice = infoObj.isDevice.toString()
        this.editForm.factoryType = infoObj.factoryType.toString()
        this.editForm.serverId = infoObj.serverId
        this.editForm.tel = infoObj.tel
        this.editForm.isEdgeCalculator = (infoObj.isEdgeCalculator||'0').toString()
      }
  },
  methods: {
    saveEdit(formName) {
        // 阻止事件冒泡
        event.stopPropagation()
        let that = this
        that.$refs[formName].validate((valid) => {
          if (valid) {
            let parames = that.editForm
            parames.factoryType = that.editForm.isDevice==0?0:parames.factoryType
            update(parames).then(res => {
                if(res.status === 200) {
                    that.$message({
                        message: '保存成功！',
                        type: 'success'
                    })
                    that.closeDialog(true)
                } else {
                    that.$message({
                        message: res.info,
                        type: 'warning'
                    })
                }
            });
          } else {
              return false
          }
        });
    },
  }
}
</script>
